import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('../views/user/Login.vue')
        },
        {
            path: '/register',
            name: 'Register',
            component: () => import('../views/user/Register.vue')
        },
        // 拍卖市场路由
        {
            path: '/market',
            name: 'Market',
            component: () => import('../views/market/Layout.vue'),
            meta: { requiresAuth: true },
            children: [
                {
                    path: '',
                    name: 'MarketHome',
                    component: () => import('../views/market/Home.vue')
                },
                {
                    path: 'category/:categoryId',
                    name: 'CategoryAuctions',
                    component: () => import('../views/market/CategoryAuctions.vue')
                },
                {
                    path: 'auction/:id',
                    name: 'AuctionDetail',
                    component: () => import('../views/market/AuctionDetail.vue')
                }
            ]
        },
        // 用户路由
        {
            path: '/user',
            name: 'UserHome',
            component: () => import('../components/Layout.vue'),
            children: [
                // 用户首页/控制面板
                {
                    path: '',
                    name: 'UserDashboard',
                    component: () => import('../views/user/Dashboard.vue')
                },
                // 用户个人信息
                {
                    path: 'profile',
                    name: 'UserProfile',
                    component: () => import('../views/user/UserInfo.vue'),
                    meta: { title: '个人信息', requiresAuth: true }
                },
                // 用户地址管理
                {
                    path: 'address',
                    name: 'UserAddress',
                    component: () => import('../views/user/AddressBook.vue'),
                    meta: { title: '地址管理', requiresAuth: true }
                },
                // 我的竞拍记录
                {
                    path: 'bids',
                    name: 'UserBids',
                    component: () => import('../views/user/UserBids.vue'),
                    meta: { title: '我的竞拍', requiresAuth: true }
                },
                // 拍卖品详情
                {
                    path: 'auction/:id',
                    name: 'UserAuctionDetail',
                    component: () => import('../views/market/AuctionDetail.vue'),
                    meta: { title: '拍卖品详情', requiresAuth: true }
                },
                // 我的订单
                {
                    path: 'orders',
                    name: 'UserOrders',
                    component: () => import('../views/user/UserOrders.vue'),
                    meta: { title: '我的订单', requiresAuth: true }
                },
                // 卖家申请
                {
                    path: 'seller/apply',
                    name: 'SellerApply',
                    component: () => import('../views/user/SellerApply.vue'),
                    meta: { title: '卖家申请', requiresAuth: true }
                },
                // 卖家中心
                {
                    path: 'seller',
                    name: 'SellerCenter',
                    component: () => import('../views/user/SellerCenter.vue'),
                    meta: { title: '卖家中心', requiresAuth: true, isSeller: true }
                },
                // 发布拍卖品
                {
                    path: 'seller/auction/publish',
                    name: 'PublishAuction',
                    component: () => import('../views/user/PublishAuction.vue'),
                    meta: { title: '发布拍卖品', requiresAuth: true, isSeller: true }
                },
                // 我的拍卖品
                {
                    path: 'seller/auctions',
                    name: 'MyAuctions',
                    component: () => import('../views/user/MyAuctions.vue'),
                    meta: { title: '我的拍卖品', requiresAuth: true, isSeller: true }
                },

            ],
            meta: { requiresAuth: true, userType: 'user' }
        },
        // 管理员路由
        {
            path: '/admin',
            name: 'AdminHome',
            component: () => import('../components/admin/Layout.vue'),
            children: [
                {
                    path: '',
                    name: 'AdminDashboard',
                    component: () => import('../views/admin/Dashboard.vue')
                },
                // 管理员个人信息
                {
                    path: 'profile',
                    name: 'AdminProfile',
                    component: () => import('../views/admin/AdminProfile.vue')
                },
                // 用户管理
                {
                    path: 'users',
                    name: 'UserList',
                    component: () => import('../views/admin/user/UserList.vue')
                },
                // 卖家管理
                {
                    path: 'seller-applications',
                    name: 'SellerApplications',
                    component: () => import('../views/admin/seller/SellerApplications.vue')
                },
                // 卖家列表
                {
                    path: 'sellers',
                    name: 'SellerList',
                    component: () => import('../views/admin/seller/SellerList.vue')
                },
                // 拍卖品管理
                {
                    path: 'auctions',
                    name: 'AuctionList',
                    component: () => import('../views/admin/auction/AuctionList.vue'),
                    meta: { title: '拍卖管理' }
                },
                // 拍卖品类目管理
                {
                    path: 'categories',
                    name: 'CategoryList',
                    component: () => import('../views/admin/category/CategoryList.vue'),
                    meta: { title: '分类管理' }
                },
                // 订单管理
                {
                    path: 'order',
                    name: 'OrderList',
                    component: () => import('../views/admin/order/OrderList.vue'),
                    meta: { title: '订单管理', requiresAuth: true }
                },
                // 竞价管理
                {
                    path: 'bid',
                    name: 'AuctionBidList',
                    component: () => import('../views/admin/bid/AuctionBidList.vue'),
                    meta: { title: '竞价管理', requiresAuth: true }
                },
            ],
            meta: { requiresAuth: true, userType: 'admin' }
        }
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    console.log('路由跳转:', from.path, '->', to.path, '元信息:', to.meta)

    // 检查是否需要登录
    if (to.meta.requiresAuth) {
        // 根据用户类型检查是否有权限访问
        const userType = to.meta.userType
        const token = userType === 'admin'
            ? localStorage.getItem('adminToken')
            : localStorage.getItem('userToken')

        console.log('检查路由权限，用户类型:', userType, '是否有token:', !!token)

        if (!token) {
            console.warn('未找到token，重定向到登录页')
            next({ name: 'Login' })
            return
        }

        // 检查卖家权限
        if (to.meta.isSeller) {
            const userStore = JSON.parse(localStorage.getItem('user') || '{}')
            if (!userStore.isSeller) {
                console.warn('非卖家用户，重定向到卖家申请页面')
                next({ name: 'SellerApply' })
                return
            }
        }
    }
    next()
})

export default router